package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

public class LibraryManager {

    private final static String WELCOME = "Welcome";
    private List<LibraryBook> booklist = newArrayList(new LibraryBook("Catcher in the Rye", true),
                                                     new LibraryBook("The Hobbit", true));

    private ConsolePrinter consolePrinter = new ConsolePrinter(System.out);
    private MainMenu mainMenu = new MainMenu(consolePrinter);
    private ConsoleReader consoleReader = new ConsoleReader(new BufferedReader(new InputStreamReader(System.in)));

    public LibraryManager(ConsolePrinter consolePrinter, ConsoleReader consoleReader, List<E> Books) {
        this.consoleReader = consoleReader;
        this.consolePrinter = consolePrinter;
        mainMenu = new MainMenu(consolePrinter);
    }

    public void go(){

        displayWelcomeScreen();
        getUserMenuOption();

    }

    private void getUserMenuOption() {
        int input = -1;
        try {
            input = Integer.parseInt(consoleReader.readLn());
        } catch (Exception e) {
            e.printStackTrace();
        }
        mainMenu.validateInput(input);
    }

    public void displayWelcomeScreen() {
        consolePrinter.print(WELCOME);
        List<String> books = transformBookListIntoStrings(booklist);
        consolePrinter.print(books);
        mainMenu.display();
    }

    private List<String> transformBookListIntoStrings(List<LibraryBook> booklist) {
        List<String> list = new ArrayList<String>();
        for(LibraryBook book : booklist) {
            list.add(book.getTitle());
        }
        return list;
    }

}
